package com.example.meterialproject.view.behavior.demo1

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.example.meterialproject.exts.name
import com.example.meterialproject.exts.szj.szj

/**
 *
 * @ClassName: ColorBehavior
 * @Author: android 超级兵
 * @CreateDate: 4/13/22$ 2:29 PM$
 * TODO
 */
class ColorBehavior(val context: Context, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<AppCompatImageView>(context, attrs) {

    companion object {
        const val TAG = "szjColorBehavior"
    }

    /*
     * 作者:android 超级兵
     * 创建时间: 4/22/22 8:05 PM
     * TODO 判断跟随变化的 View
     * @param parent: CoordinatorLayout
     * @param child: 当前的 view
     * @param dependency: 需要依赖的 View
     */
    override fun layoutDependsOn(
        parent: CoordinatorLayout,
        child: AppCompatImageView,
        dependency: View
    ): Boolean {
        return true
    }

    // 改变当前的状态
    override fun onDependentViewChanged(
        parent: CoordinatorLayout,
        child: AppCompatImageView,
        dependency: View
    ): Boolean {
        Log.i(TAG, "dependency:${dependency.name}")
        // 随机颜色
        child.setBackgroundColor(context.szj.randomColor())
        return super.onDependentViewChanged(parent, child, dependency)
    }



    override fun getScrimColor(parent: CoordinatorLayout,
                               child: AppCompatImageView): Int {
        return Color.RED
    }

    override fun getScrimOpacity(parent: CoordinatorLayout,
                                 child: AppCompatImageView): Float {
        return 0.5f
    }
}